/*require("ui.js")
 * */
var PKDialog = (function(){
    var id_ui = ".pkDialog";
    var id_okButton = ".pkDialog .okButton";
    var id_cancelButton = ".pkDialog .cancelButton";
    var id_tableContent = ".pkDialog #tableContent";
    var id_record = ".pkDialog tr.record";
    return {
        name:"PKDialog",
        hide:function(){
            addClassWithCheck(id_ui,"hide");
        },
        show:function(){
            removeClassWithCheck(id_ui,"hide");
            PKDialog._registerDialogEvents();
        },
        clear:function(){
            $(id_tableContent).html("");
        },
        appendRecord:function(name,status,level,win,lost,drop,rate,id){
            var html = '<td class="name">'+name + '</td>';
            html += ('<td class="status">'+ status + '</td>');
            html += ('<td class="level">'+ level + '</td>');
            html += ('<td class="win">'+ win + '</td>');
            html += ('<td class="lost">'+ lost + '</td>');
            html += ('<td class="drop">'+ drop + '</td>');
            html += ('<td class="rate">'+ (rate*100).toFixed(2) + '%</td>');
            html += ('<td class="playerId">'+ id + '</td>');
            html = '<tr class="record">' + html + '</tr>';
            $(id_tableContent).append(html);
            $(id_record).dblclick(function(){
                    var enemyId = $(this).children("td:last").text();
                    var enemyName = $(this).children("td:first").text();
                    if(app.getSession().getPlayer().id !== enemyId){
                        UISystem.messageBox("是否要向" + enemyName + "发起挑战",function(){
                            app.getSession().request(new RequestBattleRequest(enemyId),RequestBattleCB);
                        });
                    }
            });
        },
        delRecordByName:function(name){
            $(id_tableContent+" tr").each(function(){
                if(($(this).children("td:first").text())===name){
                    $(this).remove();
                }
            });
        },
        delRecordById:function(id){
            $(id_tableContent+" tr").each(function(){
                if(($(this).children("td:last").text())===id){
                    $(this).remove();
                }
            });
        },
        _registerDialogEvents:function(){
            $(id_okButton).click(function(){
                PKDialog.hide();
            });
            $(id_cancelButton).click(function(){
                PKDialog.hide();
            });
        }
    }
})();

UISystem.registerDialog(PKDialog.name,PKDialog);

var CommonDialog = (function(){
    var id_ui = ".commonDialog";
    var id_text = ".commonDialog .dialogText";
    var id_cancelButton = ".commonDialog .cancelButton";
    var id_okButton = ".commonDialog .okButton";
    return {
        name:"CommonDialog",
        hide:function(){
            addClassWithCheck(id_ui,"hide");
        },
        show:function(){
            removeClassWithCheck(id_ui,"hide");
        },
        clear:function(){
            $(id_text).html("");
        },
        setText:function(text){
            $(id_text).html(text);
        },
        onOK:function(cb){
            $(id_okButton).click(function(){
                if(typeof cb === "function"){
                    cb();
                }
                CommonDialog.hide();
            });
        },
        onCancel:function(cb){
            $(id_cancelButton).click(function(){
                if(typeof cb === "function"){
                    cb();
                }
                CommonDialog.hide();
            });
        },
    }
})();

UISystem.registerDialog(CommonDialog.name,CommonDialog);

UISystem.messageBox = function(text,okCB,cancelCB){
    var dialog = UISystem.getDialog("CommonDialog");
    dialog.setText(text);
    if(typeof okCB === "function"){
        dialog.onOK(okCB);
    }else{
        dialog.onOK(function(){dialog.hide();});
    }
    if(typeof cancelCB === "function"){
        dialog.onCancel(cancelCB);
    }else{
        dialog.onCancel(function(){dialog.hide();});
    }
    dialog.show();
};

